using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CodeStory.Core.UI;
using CodeStory.Core;
using CodeStory.Core.ObjectEntity;

namespace CodeStory.WebApplication.Modules.Core.Admin
{
	[NavigationPortlet("CodeStory_Core_Admin_CreateNote")]
	public partial class CreateNote : Portlet
	{
		protected override void OnInit(EventArgs e) {
			Load += new EventHandler(CreateNote_Load);
			btnCreate.Click += new EventHandler(ButtonCreate_Click);
			base.OnInit(e);
		}

		void ButtonCreate_Click(object sender, EventArgs e) {
			NoteObject note = null;
			string noteid = Request.QueryString["noteid"];
			if (string.IsNullOrEmpty(noteid)) {
				note = NoteObject.New();
				note.NoteID = Guid.NewGuid();
			} else {
				note = NoteObject.Read(new Guid(noteid));
			}
			note.NoteTitle = txtTitle.Text;
			note.NoteContent = txtContent.Text;
			note.Save();
		}

		void CreateNote_Load(object sender, EventArgs e) {
			if (!IsPostBack) {
				BindNote();
			}
		}

		private void BindNote() {
			string noteid = Request.QueryString["noteid"];
			if (string.IsNullOrEmpty(noteid)) return;
			NoteObject n = NoteObject.Read(new Guid(noteid));
			txtTitle.Text = n.NoteTitle;
			txtContent.Text = n.NoteContent;
		}
	}
}